/*
 * Copyright Notice ====================================================
 * This file contains proprietary information of Hewlett-Packard Co.
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) 2013 All rights reserved. =============================
 */

package com.hp.ecs.sonar.util;

public class ScmUtil {

	public static String getNewLine(String conent) {
		String[] lines = conent.split(";");
		String recentTime = "0000-00-00";
		String newline = null;
		for (String line : lines) {
			String time = line.substring(line.indexOf('='));
			if (time.compareTo(recentTime) > 0) {
				recentTime = time;
				newline = line;
			}
		}
		return newline;
	}

	public static String trimTime(String time) {
		return trimTime(time, 8);
	}

	public static String trimTime(String time, int tail) {
		// 2013-06-11T19:31:53+0800
		if (time.length() > 20) {
			return time.substring(0, time.length() - tail);
		}
		return time;
	}

	public static final char LINE_SPLIT = '\n';

	public static String subLines(String text, int startLine, int lineCount) {
		StringBuilder sb = new StringBuilder();
		int count = 0;
		for (char c : text.toCharArray()) {
			if (c == LINE_SPLIT) {
				count++;
				if (count == startLine + lineCount) {
					break;
				}
			}
			if (count >= startLine) {
				sb.append(c);
			}
		}
		return sb.toString().substring(1);
	}

	public static Integer getLineNo(String line) {
		return Integer.valueOf(line.substring(0, line.indexOf('=')));
	}

}
